package com.zm.cc.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zm.cc.Result;
import com.zm.cc.Utils;
import com.zm.cc.entity.User;
import com.zm.cc.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class PwdUpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        User user = (User) req.getSession().getAttribute("loginUser");
        Result result = new Result();
//        if(user == null){
//            result.setCode(-1);
//            result.setMsg("请先登录");
//            Utils.print(resp,result);
//            return;
//        }

        User user = new User();
        user.setId(1);
        user.setUname("zm");
        user.setPwd("123");
        String oldPwd = req.getParameter("oldPwd");
        String newPwd = req.getParameter("newPwd");
        if(!(user.getUname().equals(oldPwd))){
            result.setCode(-2);
            result.setMsg("原密码错误");
            Utils.print(resp,result);
            return;
        }
        ApplicationContext cxt = (ApplicationContext) this.getServletContext().getAttribute("cxt");
        UserServiceImpl dao = cxt.getBean(UserServiceImpl.class);

        user.setPwd(newPwd);
        boolean state = dao.updateById(user);
        if(state){
            result.setCode(1);
            result.setMsg("修改成功");
            Utils.print(resp,result);
            req.getSession().setAttribute("loginUser",user);
            return;
        }

        result.setCode(0);
        result.setMsg("修改失败，系统繁忙");
        Utils.print(resp,result);

    }
}
